import 'package:flutter/material.dart';

/////// FadeAnimationDemoSampleApp

class FadeAnimationDemoSampleApp extends StatelessWidget {
	// This widget is the root of your application.
	@override
	Widget build(BuildContext context) {
		return MaterialApp(
			title: 'Fade Demo',
			theme: ThemeData(
				primarySwatch: Colors.blue,
			),
			home: FadeAnimationDemo(title: 'Fade Demo'),
		);
	}
}

class FadeAnimationDemo extends StatefulWidget {
	FadeAnimationDemo({Key key, this.title}) : super(key: key);

	final String title;

	@override
	_FadeAnimationDemo createState() => _FadeAnimationDemo();
}

class _FadeAnimationDemo extends State<FadeAnimationDemo> with TickerProviderStateMixin {
	AnimationController controller;
	CurvedAnimation curve;

	@override
	void initState() {
		controller = AnimationController(
			duration: const Duration(milliseconds: 1000),
			vsync: this
		);
		curve = CurvedAnimation(parent: controller, curve: Curves.easeIn);
	}

	@override
	Widget build(BuildContext context) {
		return Scaffold(
			appBar: AppBar(
				title: Text('Fade Demo'),
			),
			body: Center(
				child: Column(
					children: <Widget>[
						_fadeInWidget(),
						_rotationWidget(),
					],
				)
			),
			floatingActionButton: FloatingActionButton(
				tooltip: 'Fade',
				child: Icon(Icons.brush),
				onPressed: () {
					if (controller.isCompleted) {
						controller.reverse();
					} else {
						controller.forward();
					}
				},
			),
		);
	}

	Widget _fadeInWidget() {
		return GestureDetector(
			onDoubleTap: () {
				if (controller.isCompleted) {
					controller.reverse();
				} else {
					controller.forward();
				}
			},
			child: Row(
				children: <Widget>[
					Text("FadeTransition"),
					FadeTransition(
						opacity: curve,
						child: FlutterLogo(
							size: 100.0,
						)
					),
				],
			),
		);
	}

	Widget _rotationWidget() {
		return GestureDetector(
			onDoubleTap: () {
				if (controller.isCompleted) {
					controller.reverse();
				} else {
					controller.forward();
				}
			},
			child: Row(
				children: <Widget>[
					Text("RotationTransition"),
					RotationTransition(
						turns: curve,
						child: FlutterLogo(
							size: 100.0,
						)
					),
				],
			),
		);
	}


	@override
	dispose() {
		controller.dispose();
		super.dispose();
	}
}
